﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=1">
    <meta name="format-detection" content="telephone=no">
    <meta charset="UTF-8">
    <title>todo完整案例</title>
    <link rel="stylesheet" href="../css/bootstrap.css"/>
    <script src="../js/jquery-2.2.0.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <style>
        .del{text-decoration: line-through; color:#cccccc;}
    </style>
</head>
<body>
<h1>todo完整案例</h1><br/>

<div id="app">
    {{name}}<br/>
    <nav class="navbar navbar-inverse">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">
                    ToDoList
                </a>
            </div>
        </div>
    </nav>

    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-warning">
                    <div class="panel-heading">
                        <h3 class="text-danger">亲~ ^_^你还有 {{count}} 件事要完成</h3>
                        <input v-model="title" @keyup.13="add" type="text" class="form-control" placeholder="回车添加新数据">
                    </div>
                    <div class="panel-body">
                        <ul class="list-group">
                            <li class="list-group-item" v-for="(todo,i) in todos" @click="remember(todo)">
                                <span :class="{del:todo.isSelected}">
                                    <input type="checkbox" v-model="todo.isSelected">
                                    <span v-show="cur!=todo">{{todo.title}}</span>
                                    <input v-focus="cur==todo" type="text" v-model="todo.title" v-show="cur==todo" @keyup.enter="cur={}" @blur="cur={}">
                                </span>

                                <button class="pull-right btn btn-danger btn-xs" @click="removeItem(todo)">&bigotimes;</button>
                            </li>
                        </ul>
                    </div>
                    <div class="panel-footer">
                        <ul class="nav nav-pills">
                            <li role="presentation"><a href="javascript:void(0)">全部任务</a></li>
                            <li role="presentation"><a href="javascript:void(0)">已完成</a></li>
                            <li role="presentation"><a href="javascript:void(0)">未完成</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../js/vue-2.5.16.js"></script>
<script src="../node_modules/axios/dist/axios.js"></script>
<script src="../node_modules/lodash/lodash.js"></script>

<script>
    var app = new Vue({
        el:'#app',
        directives:{
            focus(el,bindingParam){
                if(bindingParam.value){
                    //focus方法比show方法先一步，而当时input还没显示，所以会觉得focus()没生效
                    //解决方法：让input先show出来再focus
                    el.style.display = 'block';
                    el.focus();//el是一个dom元素，让它获取焦点
                }
            }
        },
        data:{
            name:'模版数据',
            todos:[
                {isSelected:false,title:'吃饭'},
                {isSelected:false,title:'睡觉'},
                {isSelected:false,title:'打豆豆'}
            ],
            title:'',
            cur:''
        },
        methods:{
            add(){
                this.todos.push({isSelected:false,title:this.title});
                this.title = '';
            },
            removeItem(todo){
                this.todos = this.todos.filter((item,i)=> item!=todo);
            },
            remember(todo){
                this.cur = todo;
            }
        },
        computed:{
            count(){
                return this.todos.filter(item=>!item.isSelected).length;
            }
        }
    });
</script>

</body>
</html>